const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
const {
  isNull
} = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    evaluatelist: [1, 1, 1, 1, 0.5],
    score: 0,
    page: 1,
    commentList: [],
    comment: '',
    height: 0,
    storeReviewsId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    });
    this.getStoreScore();
    this.getStoreCommentList();
  },
  // （评价管理）获取门店总评价接口
  getStoreScore: function () {
    let userId = wx.getStorageSync('userId');
    let storeId = wx.getStorageSync('storeId');
    _request.get('/storeManage/getStoreScore', {
      userId: userId,
      storeId: storeId,
    }).then((res) => {
      this.setData({
        score: res.data.data.score,
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  // （评价管理）获取用户评价列表
  getStoreCommentList: function () {
    let storeId = wx.getStorageSync('storeId');
    _request.get('/storeComment/getStoreCommentList', {
      storeId: storeId,
      page: this.data.page,
      size: 20
    }).then((res) => {
      var commentList = res.data.data;
      for (let i = 0; i < commentList.length; i++) {
        commentList[i]['createtime'] = (commentList[i]['createtime']).substring(0, 10);
        commentList[i]['storerepay'] = '';
        commentList[i]['bool'] = commentList[i]['comment'].length >= 60 ? true : false;
        commentList[i]['stylistReply'] = decodeURIComponent(commentList[i]['stylistReply']);
        commentList[i]['reply'] = decodeURIComponent(commentList[i]['reply']);
      }
      var listTemps = this.data.commentList;
      for (let i = 0; i < commentList.length; i++) {
        listTemps.push(commentList[i]);
      }
      this.setData({
        commentList: listTemps,
      });
    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    wx.previewImage({
      current: item[index], //当前图片地址
      urls: item, //所有要预览的图片的地址集合 数组形式
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  // 查看全文
  toMore(e) {
    let index = e.currentTarget.dataset.index;
    var commentList = this.data.commentList;
    commentList[index]['showtext'] = commentList[index]['showtext'] ? false : true;
    this.setData({
      commentList: commentList
    })
  },
  commentblur: function (event) {

    const list = this.data.commentList
    let storeReviewsId = event.currentTarget.dataset.id;
    list.forEach(e => {
      if (e.id === storeReviewsId) {
        e.storerepay = event.detail.value
      }
    })

    this.setData({
      commentList: list
    })
  },
  // 回复
  onSendTap: function (e) {
    let storeReviewsId = e.currentTarget.dataset.item;
    const self = this
    const list = self.data.commentList
    let comment = ''
    list.forEach(e => {
      if (e.id === storeReviewsId) {
        comment = e.storerepay
      }
    })
    if(!comment || isNull(comment)){
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
     //将emoji表情进行转义再发送至后台
    let regex = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig
    
    let commentNew = comment.replace(regex, function (res) {
      return encodeURIComponent(res)
    })

    _request.postObj('/storeComment/replyStoreComment', {
      storeReviewsId: storeReviewsId,
      context: commentNew,
    }).then((res) => {

      if (res.data.code === 200) {
        const list = self.data.commentList
        list.forEach(e => {
          if (e.id === storeReviewsId) {
            e.reply = comment
          }
        })

        self.setData({
          commentList: list,
          storeReviewsId: storeReviewsId
        })
      } else {
        console.log(res)
        if (res.data.message.indexOf('敏感词') > -1) {
          wx.showToast({
            title: '包含敏感词汇、无效回复',
            icon: 'error',
            duration: 2000
          })
        }
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  },
  more: function () {
    console.log('加载更多');
    this.data.page++;
    this.getStoreCommentList();
  },
  timestampToTime: function (timestamp) {
    var date = new Date(timestamp);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
  }

})